#!/bin/sh
#
# Usage:
#	./release-sh  [版本号]  [硬件架构]


# 检查从命令行指定的版本号参数
if [ -n "$1" ] ; then
	VERSION=$1
else
	VERSION=`date '+%Y%m%d'`
fi


# 检查目标机硬件架构
if [ -n "$2" ] ; then
	ARCH=$2
else
	ARCH=`uname --machine` # 未指定硬件架构时，自动为本机进行编译
fi

CC=cc
case $ARCH in
    i[3-6]86 ) # 32位PC机
	CFLAGS="-march=$ARCH"
	break
	;;
    x86_64|amd64) # AMD64位PC机
	echo "Warning: Build for $ARCH has not been fully supported yet."
	echo "Contact the authors for more infomation. (See ReadMe.html)"
	CFLAGS="-m64 -D NDEBUG"
	break
	;;
    mips) # MIPS内核的路由器(或龙芯PC机?)
	echo "Warning: Build for $ARCH has not been fully supported yet."
	echo "Contact the authors for more infomation. (See ReadMe.html)"
	CC=mipsel-linux-uclibc-gcc
	CFLAGS="-D NDEBUG"
	break
	;;
    arm[7,9])	# 为ARM 7、9编译
	;;	# （尚未实现）
    ia64)	# 为Intel安腾64位机编译
	;;	# （尚待实现）
    *)	# 其他未知的硬件架构
	echo "Sorry! Build for $ARCH architecture has not been fully supported yet."
	echo "Maybe you can help us handling this problem?"
	echo "Contact the authors for more infomation. (See ReadMe.html)"
	echo "Suggestions will always be welcomed!"
	exit 255
	;;
esac

SRCFILE=ZDClient-$VERSION-src.tar.gz
SRC_AR=ZDClient-$VERSION-src
PACKAGE=ZDClient-$VERSION-$ARCH
SOURCE_DIR=`pwd`
OUTPUT_DIR=`pwd`/$PACKAGE


mkdir $SRC_AR
cp $SOURCE_DIR/* $SRC_AR
tar cvfa $SRCFILE $SRC_AR

echo "Package name: $PACKAGE"
echo "Source directory: $SOURCE_DIR"
echo "Output directory: $OUTPUT_DIR"
echo "CC=$CC"
echo "CFLAGS=\"$CFLAGS\""
echo "LDFLAGS=\"$LDFLAGS\""
echo "LDLIBS=\"$LDLIBS\""

# 从输出目录调用make进行编译
echo "Building..."
mkdir -p $OUTPUT_DIR && \
cd $OUTPUT_DIR       && \
env VPATH="$SOURCE_DIR" CC=$CC CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" LDLIBS="$LDLIBS"	\
make all clean --quiet --always-make --makefile=$SOURCE_DIR/Makefile

if [ $? -eq 0 ] ; then
	echo "Build Successful."
else
	echo "Build Faild!"
	exit 255
fi


# 压缩打包
TARBALL=$PACKAGE-bin.tar.gz
cd $OUTPUT_DIR/..	&& \
cp $SOURCE_DIR/dhcp_zdc_run.sh $SOURCE_DIR/st_zdc_run.sh $SOURCE_DIR/Readme.txt $SOURCE_DIR/changelog\
 $SOURCE_DIR/$SRCFILE $OUTPUT_DIR/
tar cvfz $TARBALL $PACKAGE
rm -rf $PACKAGE/ $SRC_AR/

if [ $? -eq 0 ] ; then
	echo "Created tarball: $TARBALL"
else
	echo "Error: Failed to create tarball."
	exit 255
fi
